//
//  NSArray+DeepCopy.m
//  TestCopy
//
//  Created by YC on 2017/6/12.
//  Copyright © 2017年 YC. All rights reserved.
//

#import "NSArray+DeepCopy.h"

@implementation NSArray (DeepCopy)

- (NSArray *)deepCopy {
    NSMutableArray *array = [NSMutableArray array];
    
    for (id element in self) {
        id copyElement = nil;
        if ([element respondsToSelector:@selector(deepCopy)]) {
            copyElement = [element deepCopy];
        }
        else if ([element respondsToSelector:@selector(copyWithZone:)]) {;
            copyElement = [element copy];
        }
        else {
            copyElement = element;
        }
        [array addObject:copyElement];
    }
    
    NSArray *result = [NSArray arrayWithArray:array];
    return result;
}

- (NSMutableArray *)mutableDeepCopy {
    NSMutableArray *array = [NSMutableArray array];
    for (id element in self) {
        id copyElement = nil;
        if ([element respondsToSelector:@selector(mutableDeepCopy)]) {
            copyElement = [element mutableDeepCopy];
        }
        else if ([element respondsToSelector:@selector(mutableCopyWithZone:)]) {
            copyElement = [element mutableCopy];
        }
        else if ([element respondsToSelector:@selector(copyWithZone:)]) {
            copyElement = [element copy];
        }
        else {
            copyElement = element;
        }
        [array addObject:copyElement];
    }
    return array;
}

@end
